﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;


namespace Crawler
{

    class Crawler
    {
        string[] proxyStrs;

        public void GetProxys(string url)
        {
            string html;

            var request = (HttpWebRequest)HttpWebRequest.Create(url);

            // 代理设置：
            WebProxy proxy = new WebProxy();

            request.Accept = "*/*";
            request.UserAgent = "Mozilla/4.0 (MSIE 6.0; Windows NT 8982 )";
            request.Headers["Accept-Language"] = "zh-cn";
            request.Timeout = 6000;
            try
            {

                HttpWebResponse oWebResp = (HttpWebResponse)request.GetResponse();
                StreamReader oStream = new StreamReader(oWebResp.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
                string sResp = oStream.ReadToEnd();
                html = sResp;
                oStream.Close();
            }
            catch
            {
                return;
            }

            #region Reg Exp

            Regex engine = new Regex("((\\d+)\\.){3}((\\d+)) ((\\d+))");

            var proxys = engine.Matches(html);

            proxyStrs = new string[proxys.Count];
            for (int i = 0; i < proxys.Count; i++)
            {
                proxyStrs[i] = proxys[i].ToString().Replace(' ', ':');
            }

            #endregion
        }


        /// <summary>
        /// Write Webpage Data
        /// </summary>
        public void WriteWebpage()
        {
            //////// Read
            FileStream fs = new FileStream("_proxy.htm", FileMode.Open, FileAccess.Read);
            StreamReader rd = new StreamReader(fs);
            string content = rd.ReadToEnd();
            rd.Close();
            fs.Close();

            string proxyData = "";
            foreach (var item in proxyStrs)
            {
                proxyData += (item + "\r\n");
            }

            content = content.Replace("PROXY_DATA", proxyData);


            //////// Write
            if (File.Exists("proxy.htm"))
                File.Delete("proxy.htm");
            fs = new FileStream("proxy.htm", FileMode.Create, FileAccess.Write);
            StreamWriter wt = new StreamWriter(fs);
            wt.Write(content);
            wt.Close();
            fs.Close();
        }

    }
}
